<?php
	error_reporting(E_ALL || ~E_NOTICE);

	$action = ""; //1---get   2---add   3---del   4----view
	 
	if($_POST['action'])
	{
		$action = $_POST['action'];
	}
	else
	{
		if($_GET['action'])
		{
			$action = $_GET['action'];
		}
		else
		{
			$action = "get";
		}
		
	}
//    file_put_contents("test.log","action: ".$action, FILE_APPEND);	 
    $myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        die('Could not connect: '.mysql_error());
	    $string = "Can't connect database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
	    return;
    }
    $db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {
	    die("Can't use database dwaccessDB : ".mysql_error());
        $string = "Can't use database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);		
		mysql_close($myconn);
		return;
	}
        
	if($action == "view")
	{
		$sqlstr="SELECT MAX(CreatedTime) FROM accessrecord WHERE accessstat = 'pass';";
		$result=mysql_query($sqlstr); 
		$accessitem = mysql_fetch_row($result);
		mysql_free_result($result);		
//		file_put_contents("test.log", "sqlstr :".$sqlstr."\n", FILE_APPEND);
		$sqlstr="SELECT cardnum,controlerNum,gatenum,tkid FROM accessrecord WHERE CreatedTime = '".$accessitem[0]."';";
		$result=mysql_query($sqlstr); 
		$accessitem1 = mysql_fetch_row($result);
		$sqlstr="select staffname,departmentname from staffbasesetting where cardnum='".$accessitem1[0]."'AND controlerNum='".$accessitem1[1]."' AND (gatenum='".$accessitem1[2]."' OR gatenum='all');";
		$result=mysql_query($sqlstr); 
		$staffitem = mysql_fetch_row($result);
		mysql_free_result($result);
//		file_put_contents("test.log", "sqlstr :".$sqlstr."\n", FILE_APPEND);
		$todaystart = strftime('%Y-%m-%d',strtotime($accessitem[0]))." 00:00:00";
		$todayend = strftime('%Y-%m-%d',strtotime($accessitem[0]))." 23:59:59";
		$sqlstr="SELECT COUNT(*) FROM accessrecord WHERE accessstat='pass' AND entrance='in' AND CreatedTime BETWEEN '".$todaystart."' AND '".$todayend."';";
		$result=mysql_query($sqlstr); 
		$todayinitem = mysql_fetch_row($result);
		mysql_free_result($result);
//		file_put_contents("test.log", "sqlstr :".$sqlstr."\n", FILE_APPEND);
		$sqlstr="SELECT COUNT(*) FROM accessrecord WHERE accessstat='pass' AND entrance='out' AND CreatedTime BETWEEN '".$todaystart."' AND '".$todayend."';";
		$result=mysql_query($sqlstr); 
		$todayoutitem = mysql_fetch_row($result);
		mysql_free_result($result);
//		file_put_contents("test.log", "sqlstr :".$sqlstr."\n", FILE_APPEND);
//		$targetPath = dirname(__FILE__). "\\pictures\\";
//		$targetFile =$accessitem1[3].".jpg";
		$echostr = "{\"staffname\":\"".$staffitem[0]."\",\"departmentname\":\"".$staffitem[1]."\",\"cardnum\":\"".$accessitem1[0]."\",\"controlerNum\":\"".$accessitem1[1]."\",\"gatenum\":\"".$accessitem1[2]."\",\"CreatedTime\":\"".$accessitem[0]."\",\"todayin\":\"".$todayinitem[0]."\",\"todayout\":\"".$todayoutitem[0]."\",\"picfilename\":\"".$accessitem1[3]."\"}";
//		$echostr = "{\"staffname\":\"".$staffitem[0]."\",\"departmentname\":\"".$staffitem[1]."\",\"cardnum\":\"".$accessitem1[0]."\",\"controlerNum\":\"".$accessitem1[1]."\",\"gatenum\":\"".$accessitem1[2]."\",\"CreatedTime\":\"".$accessitem[0]."\",\"todayin\":\"".$todayinitem[0]."\",\"todayout\":\"".$todayoutitem[0]."\"}";
		echo base64_encode($echostr);
//		file_put_contents("test.log", "echostr :".$echostr."\n", FILE_APPEND);
	}
	//关闭对数据库的连接
    mysql_close($myconn);
	return;
?>